<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.library.dao.*, com.library.entity.*" %>
<%
    // 检查用户是否登录
    User user = (User) session.getAttribute("user");
    if (user == null) {
        response.sendRedirect("../login.jsp");
        return;
    }
    
    // 获取用户借阅信息
    BorrowRecordDAO recordDAO = new BorrowRecordDAO();
    BookDAO bookDAO = new BookDAO();
    
    List<BorrowRecord> currentRecords = recordDAO.getCurrentRecordsByUserId(user.getId());
    List<BorrowRecord> historyRecords = recordDAO.getRecordsByUserId(user.getId());
    
    int currentBorrowCount = currentRecords.size();
    int totalBorrowCount = historyRecords.size();
    int overdueCount = 0;
    
    // 计算逾期数量
    Date now = new Date();
    for (BorrowRecord record : currentRecords) {
        if (record.getReturnDate().before(now)) {
            overdueCount++;
        }
    }
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>图书管理系统 - 用户中心</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }
        .header {
            background-color: #333;
            color: white;
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .header h1 {
            margin: 0;
        }
        .header a {
            color: white;
            text-decoration: none;
            margin-left: 20px;
        }
        .sidebar {
            width: 200px;
            background-color: #444;
            color: white;
            height: 100vh;
            position: fixed;
            padding-top: 20px;
        }
        .sidebar ul {
            list-style-type: none;
            padding: 0;
            margin: 0;
        }
        .sidebar li {
            padding: 10px 20px;
        }
        .sidebar li:hover {
            background-color: #555;
        }
        .sidebar a {
            color: white;
            text-decoration: none;
        }
        .main-content {
            margin-left: 200px;
            padding: 20px;
        }
        .stats-container {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 20px;
        }
        .stat-card {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 200px;
        }
        .stat-title {
            font-size: 16px;
            color: #666;
        }
        .stat-value {
            font-size: 32px;
            font-weight: bold;
            margin-top: 10px;
        }
        .welcome {
            margin-bottom: 20px;
        }
        .welcome h2 {
            margin: 0;
        }
        .welcome p {
            color: #666;
        }
        .section {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
        .section h3 {
            margin-top: 0;
        }
        .user-info {
            margin-bottom: 20px;
        }
        .user-info table {
            width: 100%;
        }
        .user-info td {
            padding: 5px;
        }
        .current-borrows table {
            width: 100%;
            border-collapse: collapse;
        }
        .current-borrows th, .current-borrows td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .current-borrows th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>图书管理系统</h1>
        <div>
            <span>欢迎，${user.username}</span>
            <a href="logout">退出登录</a>
        </div>
    </div>
    
    <div class="sidebar">
        <ul>
            <li><a href="dashboard.jsp">用户中心</a></li>
            <li><a href="book?action=list">浏览图书</a></li>
            <li><a href="borrow?action=current">当前借阅</a></li>
            <li><a href="borrow?action=history">借阅历史</a></li>
        </ul>
    </div>
    
    <div class="main-content">
        <div class="welcome">
            <h2>用户中心</h2>
            <p>您好，${user.realName}！这里是您的个人图书管理控制台。</p>
        </div>
        
        <div class="stats-container">
            <div class="stat-card">
                <div class="stat-title">当前借阅</div>
                <div class="stat-value"><%= currentBorrowCount %></div>
            </div>
            <div class="stat-card">
                <div class="stat-title">逾期图书</div>
                <div class="stat-value"><%= overdueCount %></div>
            </div>
            <div class="stat-card">
                <div class="stat-title">总借阅次数</div>
                <div class="stat-value"><%= totalBorrowCount %></div>
            </div>
        </div>
        
        <div class="section user-info">
            <h3>个人信息</h3>
            <table>
                <tr>
                    <td>用户名：</td>
                    <td>${user.username}</td>
                </tr>
                <tr>
                    <td>真实姓名：</td>
                    <td>${user.realName}</td>
                </tr>
                <tr>
                    <td>邮箱：</td>
                    <td>${user.email}</td>
                </tr>
                <tr>
                    <td>电话：</td>
                    <td>${user.phone}</td>
                </tr>
                <tr>
                    <td>注册时间：</td>
                    <td>${user.registerTime}</td>
                </tr>
            </table>
        </div>
        
        <div class="section current-borrows">
            <h3>当前借阅</h3>
            <% if (currentRecords.isEmpty()) { %>
                <p>您当前没有借阅任何图书。</p>
            <% } else { %>
                <table>
                    <tr>
                        <th>图书名称</th>
                        <th>作者</th>
                        <th>借阅日期</th>
                        <th>应还日期</th>
                        <th>操作</th>
                    </tr>
                    <% for (BorrowRecord record : currentRecords) { 
                        Book book = bookDAO.getBookById(record.getBookId());
                    %>
                    <tr>
                        <td><%= book.getTitle() %></td>
                        <td><%= book.getAuthor() %></td>
                        <td><%= DateUtil.formatDateOnly(record.getBorrowDate()) %></td>
                        <td>
                            <% if (record.getReturnDate().before(now)) { %>
                                <span style="color: red;"><%= DateUtil.formatDateOnly(record.getReturnDate()) %> (已逾期)</span>
                            <% } else { %>
                                <%= DateUtil.formatDateOnly(record.getReturnDate()) %>
                            <% } %>
                        </td>
                        <td>
                            <a href="borrow?action=return&recordId=<%= record.getId() %>">归还</a>
                        </td>
                    </tr>
                    <% } %>
                </table>
            <% } %>
        </div>
    </div>
</body>
</html>
